package 线程安全.多线程;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class copyFileUseNio {
    public static void copy(String src, String dest) throws Exception {
        FileInputStream fi = new FileInputStream(new File(src));
        FileOutputStream fo = new FileOutputStream(new File(dest));
        FileChannel inChannel = fi.getChannel();
        FileChannel outChanel = fo.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (true) {
            int eof = inChannel.read(buffer);
            if (eof == -1) {
                break;
            }
            buffer.flip();

            outChanel.write(buffer);
            buffer.clear();
        }
        inChannel.close();
        outChanel.close();
        fi.close();
        fo.close();
    }
}
